package org.nightrunner.d3.controller.management;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.CTE;
import org.nightrunner.d3.entity.Category;
import org.nightrunner.d3.entity.Equipment;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.service.ICTEService;
import org.nightrunner.d3.service.ICategoryService;
import org.nightrunner.d3.service.IEquipmentService;
import org.nightrunner.d3.util.OperateInfo;
import org.nightrunner.d3.util.OperateInfo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;

/**
 * Management Controller
 * 
 * @author haiyang.chen
 * @date 2012-5-15
 * @version 1.0
 */
@Controller
@RequestMapping("management/cte/")
public class CategoryToEquipmentManagementController implements CRUDConstant {

	public static final Logger LOGGER = SysLog.CONTROLLER;

	private static final String RETURN_VIEW_PREFIX = "management/cte/";

	private static final String LINK_INDEX = "/management/category/index";

	private static final String LINK_NAME_INDEX = "分类管理首页";

	@Autowired
	public ICategoryService categoryService;

	@Autowired
	public IEquipmentService equipmentService;

	@Autowired
	public ICTEService CTEService;

	@RequestMapping(value = "delete/{cteId}", method = RequestMethod.GET)
	public String delete(@PathVariable("cteId") String cteId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {

		CTE cte = CTEService.getCTE(cteId);

		CTEService.deleteCTE(cte);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_DELETE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));

		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	public ICTEService getCTEService() {
		return CTEService;
	}

	public void setCTEService(ICTEService mMCService) {
		CTEService = mMCService;
	}

	@RequestMapping(value = "preadd/{categoryId}", method = RequestMethod.GET)
	public String preAdd(@PathVariable("categoryId") String categoryId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {

		Category category = categoryService.getCategory(categoryId);

		List<Equipment> matters = equipmentService.getAllEquipment();

		modelMap.addAttribute("matters", matters);
		modelMap.addAttribute("matterCategory", category);
		return RETURN_VIEW_PREFIX + VIEW_ADD;
	}

	@RequestMapping(value = "add/{categoryId}_{matterId}", method = RequestMethod.GET)
	public String add(@PathVariable("categoryId") String categoryId,
			@PathVariable("matterId") String matterId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {

		Equipment matter = equipmentService.getEquipment(matterId);

		Category matterCategory = categoryService.getCategory(categoryId);

		CTE cte = new CTE();

		cte.setEquipment(matter);
		cte.setCategory(matterCategory);

		matterCategory.getCTEs().add(cte);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo("操作成功");
		operateInfo.addLink(new Link("首页", "../index"));

		modelMap.addAttribute("operateInfo", operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "ajaxadd/{categoryId}_{equipmentId}", method = RequestMethod.GET)
	@ResponseBody
	public String ajaxAdd(@PathVariable("categoryId") String categoryId,
			@PathVariable("equipmentId") String equipmentId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {

		Equipment equipment = equipmentService.getEquipment(equipmentId);
		Category category = categoryService.getCategory(categoryId);
		JsonObject object = new JsonObject();

		List<Category> categorys = CTEService.getCategorys(equipment);

		if (!categorys.contains(category)) {
			CTE cte = new CTE();

			cte.setEquipment(equipment);
			cte.setCategory(category);

			CTEService.addCTE(cte);

			object.addProperty(CRUD_SUCCESS, true);
			object.addProperty(CRUD_INFO, "添加成功");
		} else {
			object.addProperty(CRUD_ERROR, true);
			object.addProperty(CRUD_INFO, "已经存在");
		}
		return object.toString();
	}

	// @RequestMapping(value = "ajaxdelete/{categoryId}_{matterId}", method =
	// RequestMethod.GET)
	// @ResponseBody
	// public String ajaxDelete(@PathVariable("categoryId") String categoryId,
	// @PathVariable("matterId") String matterId,
	// HttpServletRequest request, ModelMap modelMap) throws Exception {
	//
	// Equipment matter = matterService.getEquipment(matterId);
	//
	// Category matterCategory = matterCategoryService
	// .getCategory(categoryId);
	// JsonObject object = new JsonObject();
	// if (matter.getEquipmentCategories().contains(matterCategory)) {
	// matter.getEquipmentCategories().remove(matterCategory);
	// matterService.updateEquipment(matter);
	//
	// object.addProperty(CRUD_SUCCESS, true);
	// object.addProperty(CRUD_INFO, "删除成功");
	// } else {
	// object.addProperty(CRUD_ERROR, true);
	// object.addProperty(CRUD_INFO, "不存在,无法删除");
	// }
	// return object.toString();
	// }

	@RequestMapping(value = "ajaxdelete/{cteId}", method = RequestMethod.GET)
	@ResponseBody
	public String ajaxDelete(@PathVariable("cteId") String cteId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {

		CTE cte = CTEService.getCTE(cteId);

		JsonObject object = new JsonObject();
		if (cte != null) {

			equipmentService.getEquipment(cte.getEquipment().getEquipmentId())
					.getCTEs().remove(cte);
			categoryService.getCategory(cte.getCategory().getCategoryId())
					.getCTEs().remove(cte);

			cte.setCategory(null);
			cte.setEquipment(null);
			CTEService.deleteCTE(cte);

			object.addProperty(CRUD_SUCCESS, true);
			object.addProperty(CRUD_INFO, "删除成功");
		} else {
			object.addProperty(CRUD_ERROR, true);
			object.addProperty(CRUD_INFO, "不存在,无法删除");
		}
		return object.toString();
	}

	@RequestMapping(value = "preupdate/{categoryId}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable("categoryId") String categoryId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {

		Category category = categoryService.getCategory(categoryId);

		modelMap.addAttribute("category", category);

		return RETURN_VIEW_PREFIX + VIEW_UPDATE;
	}

	@RequestMapping(value = "presearch", method = RequestMethod.GET)
	public String preSearch(HttpServletRequest request, ModelMap modelMap)
			throws Exception {
		return RETURN_VIEW_PREFIX + "search";
	}

}